<?php
    header('Content-type: application/json');

    $files = glob('data/images/*.png');
    if (empty($files)) {
        echo json_encode(['code' => 400,'url' => '']);
        exit();
    }
    sort($files);


    $record_file = __DIR__ . '/data/lastfile.txt';
    $last = file_get_contents($record_file);
    $new = end($files);

    if ($new != $last) {
        file_put_contents($record_file, $new);
        echo json_encode(['code' => 200, 'url' => $new]);
        exit();
    }

    echo json_encode(['code'=> 400,'url' => '']);
?>